home *** CD-ROM | disk | FTP | other *** search
/ The Complete Utilities To…ka 501 Killer Utilities! / 501 Killer Utilities! (Macworld July 1995).cdr / Programming / OutOfPhase1.1 Source / OutOfPhase Folder / PromotableTypeCheck.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-15  |  6.1 KB  |  205 lines  |  [TEXT/KAHL]

  1. /* PromotableTypeCheck.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "PromotableTypeCheck.h"
  31. #include "Memory.h"
  32. #include "ASTUnaryOperator.h"
  33. #include "ASTExpression.h"
  34.  
  35.  
  36. /* this routine sees if the right hand type can be promoted (if necessary) to */
  37. /* become the left hand type.  True is returned if that is the case. */
  38. MyBoolean                CanRightBeMadeToMatchLeft(DataTypes LeftType, DataTypes RightType)
  39.     {
  40.         if (LeftType == eDouble)
  41.             {
  42.                 if ((RightType == eInteger) || (RightType == eFloat) || (RightType == eFixed))
  43.                     {
  44.                         return True;
  45.                     }
  46.             }
  47.         return (LeftType == RightType);
  48.     }
  49.  
  50.  
  51. /* this routine sees if the right hand type MUST be promoted to become */
  52. /* the left hand type.  it is not allowed to call with non-compatible types */
  53. MyBoolean                MustRightBePromotedToLeft(DataTypes LeftType, DataTypes RightType)
  54.     {
  55.         /* see if we have to promote */
  56.         if (LeftType == RightType)
  57.             {
  58.                 return False;
  59.             }
  60.  
  61.         /* we have to promote, so see if we can */
  62.         ERROR(LeftType != eDouble,PRERR(ForceAbort,
  63.             "MustRightBePromotedToLeft:  types are not promotable"));
  64.         ERROR((RightType != eInteger) && (RightType != eFloat) && (RightType != eDouble)
  65.             && (RightType != eFixed),PRERR(ForceAbort,
  66.             "MustRightBePromotedToLeft:  types are not promotable"));
  67.         /* we've filtered out all other possibililities, so they must be promoted */
  68.         return True;
  69.     }
  70.  
  71.  
  72. /* perform a type promotion on an expression */
  73. struct ASTExpressionRec*    PromoteTheExpression(DataTypes OriginalType,
  74.                                     DataTypes DesiredType, struct ASTExpressionRec* OriginalExpression,
  75.                                     long LineNumber, struct TrashTrackRec* TrashTracker)
  76.     {
  77.         ASTUnaryOpRec*        UnaryOperator;
  78.         ASTExpressionRec*    ResultingExpression;
  79.  
  80.         ERROR(DesiredType != eDouble,PRERR(ForceAbort,
  81.             "PromoteTheExpression:  types are not promotable"));
  82.         switch (OriginalType)
  83.             {
  84.                 default:
  85.                     EXECUTE(PRERR(ForceAbort,"PromoteTheExpression:  unknown type"));
  86.                     break;
  87.                 case eBoolean:
  88.                 case eArrayOfBoolean:
  89.                 case eArrayOfInteger:
  90.                 case eArrayOfFloat:
  91.                 case eArrayOfDouble:
  92.                 case eArrayOfFixed:
  93.                     EXECUTE(PRERR(ForceAbort,"PromoteTheExpression:  types are not promotable"));
  94.                     break;
  95.                 case eDouble:
  96.                     EXECUTE(PRERR(ForceAbort,"PromoteTheExpression:  type promotion unnecessary"));
  97.                     break;
  98.  
  99.                 case eInteger:
  100.                 case eFloat:
  101.                 case eFixed:
  102.                     UnaryOperator = NewUnaryOperator(eUnaryCastToDouble,OriginalExpression,
  103.                         TrashTracker,LineNumber);
  104.                     if (UnaryOperator == NIL)
  105.                         {
  106.                             return NIL;
  107.                         }
  108.                     ResultingExpression = NewExprUnaryOperator(UnaryOperator,TrashTracker,
  109.                         LineNumber);
  110.                     if (ResultingExpression == NIL)
  111.                         {
  112.                             return NIL;
  113.                         }
  114.                     return ResultingExpression;
  115.             }
  116.         EXECUTE(PRERR(ForceAbort,"PromoteTheExpression:  control reached end of function"));
  117.     }
  118.  
  119.  
  120. /* make sure the type is scalar */
  121. MyBoolean                IsItAScalarType(DataTypes TheType)
  122.     {
  123.         MyBoolean            Result;
  124.  
  125.         switch (TheType)
  126.             {
  127.                 default:
  128.                     EXECUTE(PRERR(ForceAbort,"IsItAScalarType:  not a scalar type"));
  129.                     break;
  130.                 case eArrayOfBoolean:
  131.                 case eArrayOfInteger:
  132.                 case eArrayOfFloat:
  133.                 case eArrayOfDouble:
  134.                 case eArrayOfFixed:
  135.                     Result = False;
  136.                     break;
  137.                 case eBoolean:
  138.                 case eInteger:
  139.                 case eFloat:
  140.                 case eDouble:
  141.                 case eFixed:
  142.                     Result = True;
  143.                     break;
  144.             }
  145.         return Result;
  146.     }
  147.  
  148.  
  149. /* make sure it is some kind of sequenced scalar */
  150. MyBoolean                IsItASequencedScalarType(DataTypes TheType)
  151.     {
  152.         MyBoolean            Result;
  153.  
  154.         switch (TheType)
  155.             {
  156.                 default:
  157.                     EXECUTE(PRERR(ForceAbort,"IsItASequencedScalarType:  not a scalar type"));
  158.                     break;
  159.                 case eArrayOfBoolean:
  160.                 case eArrayOfInteger:
  161.                 case eArrayOfFloat:
  162.                 case eArrayOfDouble:
  163.                 case eArrayOfFixed:
  164.                 case eBoolean:
  165.                     Result = False;
  166.                     break;
  167.                 case eInteger:
  168.                 case eFloat:
  169.                 case eDouble:
  170.                 case eFixed:
  171.                     Result = True;
  172.                     break;
  173.             }
  174.         return Result;
  175.     }
  176.  
  177.  
  178. /* make sure it is some kind of indexed type */
  179. MyBoolean                IsItAnIndexedType(DataTypes TheType)
  180.     {
  181.         MyBoolean            Result;
  182.  
  183.         switch (TheType)
  184.             {
  185.                 default:
  186.                     EXECUTE(PRERR(ForceAbort,"IsItAnIndexedType:  not a scalar type"));
  187.                     break;
  188.                 case eBoolean:
  189.                 case eInteger:
  190.                 case eFloat:
  191.                 case eDouble:
  192.                 case eFixed:
  193.                     Result = False;
  194.                     break;
  195.                 case eArrayOfBoolean:
  196.                 case eArrayOfInteger:
  197.                 case eArrayOfFloat:
  198.                 case eArrayOfDouble:
  199.                 case eArrayOfFixed:
  200.                     Result = True;
  201.                     break;
  202.             }
  203.         return Result;
  204.     }
  205.